package com.richapp.launcher.log;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.richapp.launcher.busevent.EventServiceDestroy;

import de.greenrobot.event.EventBus;

/**
 * Created by jack on 15/12/9.
 */
public class LogService extends Service {

    private LoggerServer loggerServer = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);

        loggerServer = LoggerServer.getInstance();
        loggerServer.setContext(this);
        loggerServer.setRunFlag(true);//是否开启或关闭日志写入内存false为关闭true为开启
        new Thread(loggerServer).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(loggerServer != null){
            loggerServer.setRunFlag(false);
        }

        EventBus.getDefault().post(new EventServiceDestroy(LogService.class));
    }

    public void onEventMainThread(BusEventLog event){
        loggerServer.addLog(event.getType(),event.getTag(),event.getContent());
    }
}
